package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import co.gov.medellin.ssti.sub.model.vo.RepAtributoVO;

public class GenerarSelectAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		HttpSession session = request.getSession();
		// String subpro = (String) session.getAttribute("generar");
		// String sede = (String) session.getAttribute("sede");

		List lFiltros = new LinkedList();
		List<RepAtributoVO> lBeneficiario = new LinkedList<RepAtributoVO>();
		List<RepAtributoVO> lAtributos = new LinkedList<RepAtributoVO>();

		List<RepAtributoVO> lEspecificos = new LinkedList<RepAtributoVO>();
		List<RepAtributoVO> lGenerales = new LinkedList<RepAtributoVO>();

		lFiltros = (List) session.getAttribute("ListaFiltrado");
		session.setAttribute("listafiltros", lFiltros);
		lBeneficiario = (List<RepAtributoVO>) session
				.getAttribute("listabeneficiario");
		lAtributos = (List<RepAtributoVO>) session
				.getAttribute("listaatributos");

		if ((Object) lBeneficiario != null) {
			int tamano1 = lBeneficiario.size();
			for (int i = 0; i < tamano1; i++) {
				RepAtributoVO repatriVO = (RepAtributoVO) lBeneficiario.get(i);
				String valor1 = "atrbeneficiario" + i;
				String valor2 = request.getParameter(valor1);
				if (valor2 != null) {
					RepAtributoVO repespecVO = new RepAtributoVO();
					repespecVO.setNombre(repatriVO.getNombre());
					repespecVO.setEtiqueta(repatriVO.getEtiqueta());
					lGenerales.add(repespecVO);
				}
			}
			session.setAttribute("listageneral", lGenerales);
			if ((Object) lAtributos != null) {
				int tamano = lAtributos.size();
				for (int i = 0; i < tamano; i++) {
					RepAtributoVO repatriVO = (RepAtributoVO) lAtributos.get(i);
					// Long valor1 =(repatriVO.getCodigo());
					String valor = "atributo" + i;
					String valor2 = request.getParameter(valor);
					if (valor2 != null) {
						// System.err.print("repatriVO.getEtiqueta()" +
						// repatriVO.getEtiqueta());
						RepAtributoVO repespecVO = new RepAtributoVO();
						repespecVO.setCodigo(repatriVO.getCodigo());
						repespecVO.setEtiqueta(repatriVO.getEtiqueta());
						lEspecificos.add(repespecVO);
					}
				}
				session.setAttribute("listaEspecificos", lEspecificos);
			} else {
				session.setAttribute("msg",
						"Seleccione alguna opcion en la lista de beneficios");
			}
		} else {
			session.setAttribute("msg",
					"Seleccione alguna opcion en la lista del beneficiario");
		}

		return mapping.findForward("success");
	}
}